﻿using DevExpress.XtraGrid.Demos.util;
using DXApplicationTangche.DTO;
using DXApplicationTangche.UC.下单MTO.Service;
using DXApplicationTangche.UC.全部工序.service;
using DXApplicationTangche.UC.生产计划.生产批生产.model;
using DXApplicationTangche.UC.生产计划.生产批生产.service;
using DXApplicationTangche.UC.订单一览新.订单预览新.service;
using DXApplicationTangche.UC.订单一览新.辅料.service;
using DXApplicationTangche.UC.货号管理;
using DXApplicationTangche.UC.项目成果管理.service;
using mendian;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace DXApplicationTangche.UC.生产计划.生产批生产
{
    public partial class Form生产工序 : DevExpress.XtraEditors.XtraForm
    {
        private String assertNo;//设备码
        private String code { get; set; }//工序码
        private String activityDescription { get; set; }//工序
        private String barcode { get; set; }//水洗标
        private String mnemonic;//工号
        private OrderCommitDTO返回 orderCommitDTO = new OrderCommitDTO返回();//运维口取的订单数据
        private XtraUserControl工序显示设计 uc工序显示设计;//工序显示设计
        public Form生产工序(WBSItemDTO wBSItemDTO)
        {
            InitializeComponent();
            this.assertNo = wBSItemDTO.assertNo;
            this.code = wBSItemDTO.code;
            this.activityDescription = wBSItemDTO.activityDescription;
            this.Text = wBSItemDTO.activityDescription;
            this.判断工序();
        }
        private void 判断工序()
        {
            this.ActiveControl = this.textBox水洗标;
            this.uc工序显示设计 = new XtraUserControl工序显示设计();
            this.uc工序显示设计.Dock = DockStyle.Fill;
            this.xtraTabPage工艺设计.Controls.Clear();
            this.xtraTabPage工艺设计.Controls.Add(this.uc工序显示设计);
        }
        private void textBox水洗标_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyValue == 13)
            {
                if (String.IsNullOrEmpty(this.textBox水洗标.Text.Trim()))
                {
                    return;
                }
                this.barcode = this.textBox水洗标.Text.Trim();
                this.成衣码查询();
            }
        }

        private void 成衣码查询()
        {
            this.clearTextBoxs();
            this.label消息.Text = "消息：" + this.barcode;
            this.mnemonic = 生产批生产Service.get查找设备session对应的员工(assertNo);
            this.label当前操作人.Text = "当前操作人：" + this.mnemonic;
            try
            {
                this.orderCommitDTO = 订单预览新Service.getOrderCommitDTOByBarcode(this.barcode);
                this.showTextBoxs();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
                return;
            }
            if (!String.IsNullOrEmpty(this.mnemonic))
            {
                this.textBox工号.BackColor = Color.White;
                下单MTO.Service.PlainMessageResult plainMessageResult = new 下单MTO.Service.PlainMessageResult();
                if ("生产开始".Equals(this.activityDescription))
                {
                    plainMessageResult = 生产批生产Service.initBarcodeWipInstance(this.barcode,this.mnemonic);
                }
                else
                {
                    plainMessageResult = 生产批生产Service.wipBarcodeScan(this.barcode, this.activityDescription, this.mnemonic);
                }
                //  判断前步工序是否完成
                //if (!"200".Equals(plainMessageResult.errorCode))
                //{
                //    MessageBox.Show(plainMessageResult.errorMessage);
                //    this.clearTextBoxs();
                //    this.ActiveControl = this.textBox水洗标;
                //    return;
                //}
            }
            else
            {
                this.label消息.Text = "消息：请扫描您的工号!";
                this.ActiveControl = this.textBox工号;
                this.textBox工号.BackColor = Color.Red;
            }
        }
        
        private void textBox工号_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyValue == 13)
            {
                if (String.IsNullOrEmpty(this.mnemonic))
                {
                    if (String.IsNullOrWhiteSpace(项目成果Service.get判断扫描员工码是否正确(this.textBox工号.Text.Trim())))
                    {
                        this.textBox工号.Clear();
                        MessageBox.Show("扫描员工码错误，请重新扫描");
                        this.textBox工号.BackColor = Color.Red;
                        return;
                    }
                    this.textBox工号.BackColor = Color.White;
                    this.mnemonic = this.textBox工号.Text.Trim();
                    生产批生产Service.save保存设备session(
                        this.assertNo,
                        this.mnemonic
                        );
                    this.label消息.Text = "消息：";
                    this.label当前操作人.Text = "当前操作人：" + this.mnemonic;
                }
                if (String.IsNullOrEmpty(this.barcode))
                {
                    MessageBox.Show("请扫描水洗标");
                    return;
                }
                下单MTO.Service.PlainMessageResult plainMessageResult = new 下单MTO.Service.PlainMessageResult();
                if ("生产开始".Equals(this.activityDescription))
                {
                    plainMessageResult = 生产批生产Service.initBarcodeWipInstance(this.barcode, this.mnemonic);
                }
                else
                {
                    plainMessageResult = 生产批生产Service.wipBarcodeScan(this.barcode, this.activityDescription, this.mnemonic);
                }
                //  判断前步工序是否完成
                //if (!"200".Equals(plainMessageResult.errorCode))
                //{
                //    MessageBox.Show(plainMessageResult.errorMessage);
                //    this.clearTextBoxs();
                //    this.ActiveControl = this.textBox水洗标;
                //    return;
                //}
                this.clearTextBoxs();
                this.ActiveControl = this.textBox水洗标;
            }
        }
        /// <summary>
        /// 显示信息
        /// </summary>
        private void showTextBoxs()
        {
            this.textBox订单号.Text = this.orderCommitDTO.orderNo;
            this.textBox订单数量.Text = this.orderCommitDTO.orderNumber.ToString();
            this.textBox下单时间.Text = this.orderCommitDTO.orderDate.ToString();
            this.textBox客户名称.Text = this.orderCommitDTO.customerName;
            this.textBox店铺名称.Text = this.orderCommitDTO.shopName;
            this.textBox款式名称.Text = this.orderCommitDTO.styleNameCn;
            this.textBox面料号.Text = this.orderCommitDTO.materialCode;
            this.textBox面料名称.Text = this.orderCommitDTO.materialNameCn;
            this.textBox面料颜色.Text = this.orderCommitDTO.materialColor;
            this.textBox面料成分.Text = this.orderCommitDTO.composition;
            this.uc工序显示设计.refreahData(this.code, this.barcode, this.orderCommitDTO.styleNameCn, false);
        }
        /// <summary>
        /// 清空
        /// </summary>
        private void clearTextBoxs()
        {
            this.textBox水洗标.Clear();
            this.textBox工号.Clear();
            this.textBox订单号.Clear();
            this.textBox订单数量.Clear();
            this.textBox下单时间.Clear();
            this.textBox客户名称.Clear();
            this.textBox店铺名称.Clear();
            this.textBox款式名称.Clear();
            this.textBox面料号.Clear();
            this.textBox面料名称.Clear();
            this.textBox面料颜色.Clear();
            this.textBox面料成分.Clear();
            this.label消息.Text = "消息：";
            this.uc工序显示设计.clearData();
        }

        private void simpleButton操作人退出_Click(object sender, EventArgs e)
        {
            this.clearTextBoxs();
            this.ActiveControl = this.textBox水洗标;
            this.mnemonic = "";
            生产批生产Service.save保存设备session(
                    this.assertNo,
                    this.mnemonic
                    );
            this.label当前操作人.Text = "当前操作人：" + this.mnemonic;
        }
    }
}
